﻿using LDT.ElementConfirm.PubCapacityByAtAnalysis.InterfaceHandle;
using LdtJudge.DataEngine.CallInterface.CommHelp;
using LdtJudge.DataEngine.CallInterface.ConfiCenter;
using LdtJudge.DataEngine.Common;
using LdtJudge.DataEngine.Common.XmlUtility;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.XMLConfigModel.DataStores;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace LdtDataEngin.ModuleDetail.OptimizationModule.TrialOutline
{
    public class TrialOutlineModel
    {
        /// <summary>
        /// 中间数据层
        /// </summary>
        public DataContext dataContext;
        /// <summary>
        /// 模块名称
        /// </summary>
        public string moudleName;
        public TrialOutlineModel(DataContext context, string _moudleName)
        {
            dataContext = context;
            moudleName = _moudleName;
        }

        public void GetTrialOut()
        {
            ElementConfirmInterface elementConfirmInterface = new ElementConfirmInterface()
            {
                freeSql = FreeSQLHelp.GetFreeSQL("LdtCommonManage")
            };
            string casecasueID, stage, configurl, xml;
            if (dataContext.DataLayerAns.ExtractCaseCausesList != null && dataContext.DataLayerAns.ExtractCaseCausesList.Count > 0)
            {
                casecasueID = dataContext.DataLayerAns.ExtractCaseCausesList[0].CC_ID;
            }
            else
            {
                casecasueID = "";
            }
            if (dataContext.DataLayerAns.StageProcessAnalysisModel != null && dataContext.DataLayerAns.StageProcessAnalysisModel.dataModel != null)
            {
                DataModel dataModel = XmlUtility.ConditionFirstOrDefaultDataModel(dataContext.DataLayerOpt.SourdatModleList, "", dataContext.DataLayerAns.StageProcessAnalysisModel.path);
                stage = dataModel.AttributesList.FirstOrDefault(a => a.Name == "阶段").sourceList[0].SaveValue;
            }
            else
            {
                stage = "";
            }
            if (dataContext.DataLayerOpt.OptimizationXml != null)
            {
                xml = dataContext.DataLayerOpt.OptimizationXml.OuterXml;
            }
            else
            {
                xml = "";
            }
            configurl = ConfigRetrieval.GetConfigRetrieval.GetAppSettingString("systemapi");
            if (!string.IsNullOrEmpty(casecasueID) && !string.IsNullOrEmpty(stage) && !string.IsNullOrEmpty(xml))
            {
                dataContext.InteractiveShow.LogShow("庭审提纲参数casecasueID：" + casecasueID);
                dataContext.InteractiveShow.LogShow("庭审提纲参数stage：" + stage);
                dataContext.InteractiveShow.LogShow("庭审提纲参数configurl：" + configurl);
                dataContext.InteractiveShow.LogShow("庭审提纲参数xml：" + xml);
                var trialOutline = elementConfirmInterface.GenerateTrialOutline(casecasueID, stage, configurl, xml);
                if (!string.IsNullOrEmpty(trialOutline.Id))
                {
                    DataModel dataModel = dataContext.DataLayerOpt.SourdatModleList.FirstOrDefault(a => a.ID == trialOutline.Id);

                    DataModel dataRomveModel = dataContext.DataLayerOpt.SourdatModleList.FirstOrDefault(a => a.ParentId == trialOutline.Id && a.XmlPath.IndexOf("庭审提纲") > -1);
                    if (dataRomveModel!=null)
                    {
                        XmlUtilCommon utilCommon = new XmlUtilCommon() { dataContext = dataContext };
                        List<DataModel> models=  utilCommon.SubordinateDataModelList(dataContext.DataLayerOpt.SourdatModleList, dataRomveModel);
                        if (models!=null && models.Count>0)
                        {
                            foreach (var itemModel in models)
                            {
                                dataContext.DataLayerOpt.SourdatModleList.Remove(itemModel);
                            }
                        }
                    }
                    XmlDocument xmlDocument = new XmlDocument();
                    string content= HandleNodeAttr(trialOutline.Content);
                    dataContext.InteractiveShow.LogShow("庭审提纲信息：" + trialOutline.Content);
                    xmlDocument.LoadXml(content);
                    XmlUtilCommon xmlUtilCommon = new XmlUtilCommon() { dataContext = dataContext };
                    DataSoursModel dataSoursModel = xmlUtilCommon.GetXMLByDataModel(xmlDocument);
                    DataModel model = dataSoursModel.dataModelList.FirstOrDefault(a => a.ParentId == "");
                    if (model != null)
                    {
                        model.ParentId = dataModel.ID;
                    }
                    dataContext.DataLayerOpt.SourdatModleList.AddRange(dataSoursModel.dataModelList);
                    dataContext.DataLayerOpt.OptimizationXml = xmlUtilCommon.DataModelByXml(dataContext.DataLayerOpt.SourdatModleList, "");
                }
            }
        }


        /// <summary>
        /// 去除xml所有节点下的id属性
        /// </summary>
        public string HandleNodeAttr(string xmlContent)
        {
            XDocument xDocument = XDocument.Parse(xmlContent);
            var xElementList = xDocument.Elements();
            if (xElementList != null && xElementList.Any())
            {
                foreach (var item in xElementList)
                {
                    DeleteNodeIdAttr(item);
                }
            }
            var content = xDocument.ToString();
            return content;
        }

        /// <summary>
        /// 删除节点id属性
        /// </summary>
        /// <param name="xElement"></param>
        private void DeleteNodeIdAttr(XElement xElement)
        {
            var idAttr = xElement.Attribute("id");
            if (idAttr != null)
            {
                idAttr.Remove();
            }
            var childs = xElement.Elements();
            if (childs != null && childs.Any())
            {
                foreach (XElement child in childs)
                {
                    DeleteNodeIdAttr(child);
                }
            }
        }
    }
}
